package com.example.demo.sys.service.bo;

import com.example.demo.sys.entity.User;
import com.example.demo.sys.service.annotation.UniqueUser;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

@Data
@Validated
public class UserBO {
    private Long id;
    private Long pid;
    @NotBlank(message = "密码不能为空")
    private String username;
    @NotBlank(message = "用户名不能为空")
    private String password;
    private String nickname;
    private Long phone;
    private Long createdAt;
    private Long updatedAt;

    public static UserBO convert(User item) {
        if (item == null) {
            return null;
        }
        UserBO result = new UserBO();
        BeanUtils.copyProperties(item, result);

        return result;
    }

    public static List<UserBO> convert(List<User> items) {
        List<UserBO> result = new ArrayList<>();
        for (int i = 0; i < items.size(); i++) {
            User item = items.get(i);
            if (item == null) {
                return null;
            }
            UserBO bo = new UserBO();
            BeanUtils.copyProperties(item, bo);
            result.add(bo);
        }
        return result;
    }

    public static User entity(UserBO item) {
        if (item == null) {
            return null;
        }
        User result = new User();
        BeanUtils.copyProperties(item, result);

        return result;
    }
}
